package server;
/**
 * most important file
 * @author Jaak-Alo Lukanenok
 * @author Taavi Rehem�gi
 */
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import client.HTTPClient;
import server.httpRequestHandler;

public class HTTPServer implements Runnable {
public	static int port = 1600;
  	
	String[] undelivered;// Max 100 undelivered messages...

	httpRequestHandler handler=new httpRequestHandler();
	
	public void start() {
  		
  		ServerSocket server_socket=null;
  		try {
  			server_socket = new ServerSocket(port);
  			System.out.println("httpServer running on port " + server_socket.getLocalPort());
  			while(true){
  				//System.out.println("test1")
  				//BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
  				Socket clientsocket = server_socket.accept();
  				//System.out.println("New connection accepted "+ clientsocket.getInetAddress() + ":" + clientsocket.getPort());
  				BufferedReader input =new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
  				DataOutputStream output =new DataOutputStream(clientsocket.getOutputStream());
  				
  				String printable=""; // testing purpose only
  				String data="";
  				while((printable=input.readLine())!=null){
  					//System.out.println(printable.toString());
  					data=printable.toString();
  				}
  				
  				//System.out.println(input.toString());
  				handler.handleRequest(data);
  			}      
  		}catch (IOException e) {
  			System.out.println(e);
    	}
  	}
	/*
	
  	private static void http_handler(BufferedReader input, DataOutputStream output) {
  		int method = 0; //1 get, 2 head, 0 not supported
  		String http = new String(); //a bunch of strings to hold
  		String path = new String(); //the various things, what http v, what path,
  		String file = new String(); //what file
  		String user_agent = new String(); //what user_agent
  		String line;
  		try {
			while(( line = input.readLine()) != null){
				System.out.println(line);
			}
		} catch (IOException e1) {
			System.out.println(e1.toString());
		}
  		System.out.println("3");
  		try {
  			String tmp = input.readLine(); //read from the stream
  			String tmp2 = new String(tmp);

  			tmp.toUpperCase(); //convert it to uppercase
  			System.out.println(tmp);
  			if (tmp.startsWith("GET")){ //compare it is it GET
  				method = 1;
  			} //if we set it to method 1
  			if (tmp.startsWith("HEAD")){ //same here is it HEAD
  				method = 2;
  			} //set method to 2
  			if(method==1){
  				System.out.println(tmp2);
  			}
  		}catch (Exception e){}
  	}
  	public String[] getM(){
  	
  		String[] delivered;
  		
  		delivered = this.undelivered;
  		this.undelivered = new String[100]; // empty
  		return delivered;
  	}

*/
	@Override
	public void run() {
		// TODO Auto-generated method stub
		start();
		
	}
  	
}
